package com.classroom.smart.service;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.classroom.smart.mapper.VideoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

@Service
public class DisplayService {
    @Autowired
    private VideoMapper videoMapper;

    @Transactional
    public String upload(MultipartFile videoFile) throws IOException {
        HashMap hashMap = new HashMap();
        if (videoFile!=null){
            String accessKeyId = "LTAI5tQqjmxN4FdQeweiSvMn";
            String accessKeySecret = "21xncpuDQxuHtSwnCzgNrmCaT0m5Ii";
            String title = videoFile.getOriginalFilename();
            String fileName = videoFile.getOriginalFilename();
            InputStream inputStream = videoFile.getInputStream();

            //1.上传视频的方法
            UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
            if (response.isSuccess()) {
                System.out.print("VideoId=" + response.getVideoId() + "\n");
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                System.out.print("VideoId=" + response.getVideoId() + "\n");
                System.out.print("ErrorCode=" + response.getCode() + "\n");
                System.out.print("ErrorMessage=" + response.getMessage() + "\n");
            }
            String id = response.getVideoId();
            hashMap.put("id", id);
            hashMap.put("fileName", fileName);
            return fileName;
        }
        return null;
    }
}
